// major.js
const majorApp = createApp({
    data() {
        return {
            majors: [],
            colleges: [],
            loading: false,
            error: null,
            formData: {
                majorCode: '',
                majorName: '',
                collegeCode: ''
            }
        }
    },
    methods: {
        // 加载专业数据
        async loadMajors() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/major/listAll');
                const collegeResponse = await fetch(window.contextPath+'/college/listAll');
                if (response.ok && collegeResponse.ok) {
                    this.majors = await response.json();
                    this.colleges = await collegeResponse.json();

                    // 创建学院代码到名称的映射表
                    const collegeMap = {};
                    this.colleges.forEach(college => {
                        collegeMap[college.collegeCode] = college.collegeName;
                    });

                    // 为每个专业添加对应的学院名称
                    this.majors = this.majors.map(major => ({
                        ...major,
                        collegeName: collegeMap[major.collegeCode] || '未知学院'
                    }));
                } else {
                    const errorText = await response.text();
                    this.error = '加载数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            }catch ( error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }finally {
                this.loading = false;
            }
        },
        // 添加专业信息
        async addMajor() {
            try {
                const response = await fetch(window.contextPath+'/major/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadMajors();
                    this.formData = {
                        majorCode: '',
                        majorName: '',
                        collegeCode: ''
                    };
                    closeMajorAddModal();
                } else {
                    const errorText = await response.text();
                    this.error = '添加专业信息失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 删除专业信息
        async deleteMajor(majorCode) {
            try {
                if (!confirm('确定要删除该专业信息吗？')) {
                    return;
                }
                const response = await fetch(window.contextPath+'/major/delete?majorCode=' + majorCode, {
                    method: 'DELETE'
                });
                if (response.ok) {
                    alert('删除成功')
                    await this.loadMajors();
                } else {
                    const errorText = await response.text();
                    this.error = '删除专业失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改专业信息
        async updateMajor() {
            alert('该功能正在开发中...')
        }
    },
    mounted() {
        window.majorAppInstance = this;
    }
});
majorApp.mount('#major-content');

// 显示添加专业表单
function showMajorAddModal() {
    const modal = document.getElementById('majorAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加专业表单
function closeMajorAddModal() {
    document.getElementById('majorAddModal').style.display = 'none';
}
